home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / me310.zip / UE310C.ZIP / DISPLAY.C < prev    next >
C/C++ Source or Header  |  1989-10-05  |  29KB  |  1,295 lines

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "etype.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. typedef struct    VIDEO {
  16.     int    v_flag;         /* Flags */
  17. #if    COLOR
  18.     int    v_fcolor;        /* current forground color */
  19.     int    v_bcolor;        /* current background color */
  20.     int    v_rfcolor;        /* requested forground color */
  21.     int    v_rbcolor;        /* requested background color */
  22. #endif
  23.     char    v_text[1];        /* Screen data. */
  24. }    VIDEO;
  25.  
  26. #define VFCHG    0x0001            /* Changed flag         */
  27. #define VFEXT    0x0002            /* extended (beyond column 80)    */
  28. #define VFREV    0x0004            /* reverse video status     */
  29. #define VFREQ    0x0008            /* reverse video request    */
  30. #define VFCOL    0x0010            /* color change requested    */
  31.  
  32. static VIDEO   **vscreen;               /* Virtual screen. */
  33. #if    MEMMAP == 0
  34. static VIDEO   **pscreen;               /* Physical screen. */
  35. #endif
  36.  
  37. /*
  38.  * Initialize the data structures used by the display code. The edge vectors
  39.  * used to access the screens are set up. The operating system's terminal I/O
  40.  * channel is set up. All the other things get initialized at compile time.
  41.  * The original window has "WFCHG" set, so that it will get completely
  42.  * redrawn on the first call to "update".
  43.  */
  44. PASCAL NEAR vtinit()
  45. {
  46.     register int i;
  47.     register VIDEO *vp;
  48.  
  49.     TTopen();        /* open the screen */
  50.     TTkopen();        /* open the keyboard */
  51.     TTrev(FALSE);
  52.     vscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  53.  
  54.     if (vscreen == NULL)
  55.     meexit(1);
  56.  
  57. #if    MEMMAP == 0
  58.     pscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  59.  
  60.     if (pscreen == NULL)
  61.     meexit(1);
  62. #endif
  63.  
  64.     for (i = 0; i < term.t_mrow; ++i)
  65.     {
  66.     vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  67.  
  68.     if (vp == NULL)
  69.         meexit(1);
  70.  
  71.     vp->v_flag = 0;
  72. #if    COLOR
  73.     vp->v_rfcolor = 7;
  74.     vp->v_rbcolor = 0;
  75. #endif
  76.     vscreen[i] = vp;
  77. #if    MEMMAP == 0
  78.     vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  79.  
  80.     if (vp == NULL)
  81.         meexit(1);
  82.  
  83.     vp->v_flag = 0;
  84.     pscreen[i] = vp;
  85. #endif
  86.     }
  87. }
  88.  
  89. #if    CLEAN
  90. /* free up all the dynamically allocated video structures */
  91.  
  92. PASCAL NEAR vtfree()
  93. {
  94.     int i;
  95.     for (i = 0; i < term.t_mrow; ++i) {
  96.         free(vscreen[i]);
  97. #if    MEMMAP == 0
  98.         free(pscreen[i]);
  99. #endif
  100.     }
  101.     free(vscreen);
  102. #if    MEMMAP == 0
  103.     free(pscreen);
  104. #endif
  105. }
  106. #endif
  107.  
  108. /*
  109.  * Clean up the virtual terminal system, in anticipation for a return to the
  110.  * operating system. Move down to the last line and clear it out (the next
  111.  * system prompt will be written in the line). Shut down the channel to the
  112.  * terminal.
  113.  */
  114. PASCAL NEAR vttidy()
  115. {
  116.     mlerase();
  117.     movecursor(term.t_nrow, 0);
  118.     TTflush();
  119.     TTclose();
  120.     TTkclose();
  121. }
  122.  
  123. /*
  124.  * Set the virtual cursor to the specified row and column on the virtual
  125.  * screen. There is no checking for nonsense values; this might be a good
  126.  * idea during the early stages.
  127.  */
  128. PASCAL NEAR vtmove(row, col)
  129. {
  130.     vtrow = row;
  131.     vtcol = col;
  132. }
  133.  
  134. /* Write a character to the virtual screen. The virtual row and
  135.    column are updated. If we are not yet on left edge, don't print
  136.    it yet. If the line is too long put a "$" in the last column.
  137.    This routine only puts printing characters into the virtual
  138.    terminal buffers. Only column overflow is checked.
  139. */
  140.  
  141. PASCAL NEAR vtputc(c)
  142.  
  143. int c;
  144.  
  145. {
  146.     register VIDEO *vp;    /* ptr to line being updated */
  147.  
  148.     vp = vscreen[vtrow];
  149.  
  150.     if (c == '\t') {
  151.         do {
  152.             vtputc(' ');
  153.         } while (((vtcol + taboff) % (tabsize)) != 0);
  154.     } else if (vtcol >= term.t_ncol) {
  155.         ++vtcol;
  156.         vp->v_text[term.t_ncol - 1] = '$';
  157.     } else if (c < 0x20 || c == 0x7F) {
  158.         vtputc('^');
  159.         vtputc(c ^ 0x40);
  160.     } else {
  161.         if (vtcol >= 0)
  162.             vp->v_text[vtcol] = c;
  163.         ++vtcol;
  164.     }
  165. }
  166.  
  167. /*
  168.  * Erase from the end of the software cursor to the end of the line on which
  169.  * the software cursor is located.
  170.  */
  171. PASCAL NEAR vteeol()
  172. {
  173.     register VIDEO    *vp;
  174.  
  175.     vp = vscreen[vtrow];
  176.     while (vtcol < term.t_ncol)
  177.     vp->v_text[vtcol++] = ' ';
  178. }
  179.  
  180. /* upscreen:    user routine to force a screen update
  181.         always finishes complete update     */
  182.  
  183. PASCAL NEAR upscreen(f, n)
  184.  
  185. {
  186.     update(TRUE);
  187.     return(TRUE);
  188. }
  189.  
  190. /*
  191.  * Make sure that the display is right. This is a three part process. First,
  192.  * scan through all of the windows looking for dirty ones. Check the framing,
  193.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  194.  * correct for the current window. Third, make the virtual and physical
  195.  * screens the same.
  196.  */
  197. PASCAL NEAR update(force)
  198.  
  199. int force;    /* force update past type ahead? */
  200.  
  201. {
  202.     register WINDOW *wp;
  203.  
  204. #if    TYPEAH
  205.     if (force == FALSE && typahead())
  206.         return(TRUE);
  207. #endif
  208. #if    VISMAC == 0
  209.     if (force == FALSE && kbdmode == PLAY)
  210.         return(TRUE);
  211. #endif
  212.  
  213.     /* update any windows that need refreshing */
  214.     wp = wheadp;
  215.     while (wp != NULL) {
  216.         if (wp->w_flag) {
  217.             /* if the window has changed, service it */
  218.             reframe(wp);    /* check the framing */
  219.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  220.                 updone(wp);    /* update EDITed line */
  221.             else if (wp->w_flag & ~WFMOVE)
  222.                 updall(wp);    /* update all lines */
  223.             if (wp->w_flag & WFMODE)
  224.                 modeline(wp);    /* update modeline */
  225.             wp->w_flag = 0;
  226.             wp->w_force = 0;
  227.         }
  228.         /* on to the next window */
  229.         wp = wp->w_wndp;
  230.     }
  231.  
  232.     /* recalc the current hardware cursor location */
  233.     updpos();
  234.  
  235. #if    MEMMAP
  236.     /* update the cursor and flush the buffers */
  237.     movecursor(currow, curcol - lbound);
  238. #endif
  239.  
  240.     /* check for lines to de-extend */
  241.     upddex();
  242.  
  243.     /* if screen is garbage, re-plot it */
  244.     if (sgarbf != FALSE)
  245.         updgar();
  246.  
  247.     /* update the virtual screen to the physical screen */
  248.     updupd(force);
  249.  
  250.     /* update the cursor and flush the buffers */
  251.     movecursor(currow, curcol - lbound);
  252.     TTflush();
  253.  
  254.     return(TRUE);
  255. }
  256.  
  257. /*    reframe:    check to see if the cursor is on in the window
  258.             and re-frame it if needed or wanted        */
  259.  
  260. PASCAL NEAR reframe(wp)
  261.  
  262. WINDOW *wp;
  263.  
  264. {
  265.     register LINE *lp;    /* search pointer */
  266.     register LINE *rp;    /* reverse search pointer */
  267.     register LINE *hp;    /* ptr to header line in buffer */
  268.     register LINE *tp;    /* temp debugging pointer */
  269.     register int i;        /* general index/# lines to scroll */
  270.     register int nlines;    /* number of lines in current window */
  271.  
  272.     /* figure out our window size */
  273.     nlines = wp->w_ntrows;
  274.     if (modeflag == FALSE)
  275.         nlines++;
  276.  
  277.     /* if not a requested reframe, check for a needed one */
  278.     if ((wp->w_flag & WFFORCE) == 0) {
  279.         lp = wp->w_linep;
  280.         for (i = 0; i < nlines; i++) {
  281.  
  282.             /* if the line is in the window, no reframe */
  283.             if (lp == wp->w_dotp)
  284.                 return(TRUE);
  285.  
  286.             /* if we are at the end of the file, reframe */
  287.             if (lp == wp->w_bufp->b_linep)
  288.                 break;
  289.  
  290.             /* on to the next line */
  291.             lp = lforw(lp);
  292.         }
  293.     }
  294.  
  295.     /* reaching here, we need a window refresh */
  296.     i = wp->w_force;
  297.  
  298.     /* if smooth scrolling is enabled,
  299.         first.. have we gone off the top? */
  300.     if (sscroll && ((wp->w_flag & WFFORCE) == 0)) {
  301.         /* search thru the buffer looking for the point */
  302.         tp = lp = rp = wp->w_linep;
  303.         hp = wp->w_bufp->b_linep;
  304.         while ((lp != hp) || (rp != hp)) {
  305.  
  306.             /* did we scroll downward? */
  307.             if (lp == wp->w_dotp) {
  308.                 i = nlines - 1;
  309.                 break;
  310.             }
  311.  
  312.             /* did we scroll upward? */
  313.             if (rp == wp->w_dotp) {
  314.                 i = 0;
  315.                 break;
  316.             }
  317.  
  318.             /* advance forward and back */
  319.             if (lp != hp)
  320.                 lp = lforw(lp);
  321.             if (rp != hp)
  322.                 rp = lback(rp);
  323.  
  324.             /* problems????? */
  325.             if (lp == tp || rp == tp) {
  326.                 printf("BUG IN SMOOTH SCROLL--GET DAN!\n");
  327.                 TTgetc();
  328.             }
  329.         }
  330.     /* how far back to reframe? */
  331.     } else if (i > 0) {    /* only one screen worth of lines max */
  332.         if (--i >= nlines)
  333.             i = nlines - 1;
  334.     } else if (i < 0) {    /* negative update???? */
  335.         i += nlines;
  336.         if (i < 0)
  337.             i = 0;
  338.     } else
  339.         i = nlines / 2;
  340.  
  341.     /* backup to new line at top of window */
  342.     lp = wp->w_dotp;
  343.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  344.         --i;
  345.         if (i < 0) {
  346.             printf("OTHER BUG IN DISPLAY --- GET DAN!!!\n");
  347.             TTgetc();
  348.         }
  349.         lp = lback(lp);
  350.     }
  351.  
  352.     /* and reset the current line at top of window */
  353.     wp->w_linep = lp;
  354.     wp->w_flag |= WFHARD;
  355.     wp->w_flag &= ~WFFORCE;
  356.     return(TRUE);
  357. }
  358.  
  359. /*    updone: update the current line to the virtual screen        */
  360.  
  361. PASCAL NEAR updone(wp)
  362.  
  363. WINDOW *wp;    /* window to update current line in */
  364.  
  365. {
  366.     register LINE *lp;    /* line to update */
  367.     register int sline;    /* physical screen line to update */
  368.     register int i;
  369.  
  370.     /* search down the line we want */
  371.     lp = wp->w_linep;
  372.     sline = wp->w_toprow;
  373.     while (lp != wp->w_dotp) {
  374.         ++sline;
  375.         lp = lforw(lp);
  376.     }
  377.  
  378.     /* and update the virtual line */
  379.     vscreen[sline]->v_flag |= VFCHG;
  380.     vscreen[sline]->v_flag &= ~VFREQ;
  381.     taboff = wp->w_fcol;
  382.     vtmove(sline, -taboff);
  383.     for (i=0; i < llength(lp); ++i)
  384.         vtputc(lgetc(lp, i));
  385. #if    COLOR
  386.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  387.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  388. #endif
  389.     vteeol();
  390.     taboff = 0;
  391. }
  392.  
  393. /*    updall: update all the lines in a window on the virtual screen */
  394.  
  395. PASCAL NEAR updall(wp)
  396.  
  397. WINDOW *wp;    /* window to update lines in */
  398.  
  399. {
  400.     register LINE *lp;    /* line to update */
  401.     register int sline;    /* physical screen line to update */
  402.     register int i;
  403.     register int nlines;    /* number of lines in the current window */
  404.  
  405.     /* search down the lines, updating them */
  406.     lp = wp->w_linep;
  407.     sline = wp->w_toprow;
  408.     nlines = wp->w_ntrows;
  409.     if (modeflag == FALSE)
  410.         nlines++;
  411.     taboff = wp->w_fcol;
  412.     while (sline < wp->w_toprow + nlines) {
  413.  
  414.         /* and update the virtual line */
  415.         vscreen[sline]->v_flag |= VFCHG;
  416.         vscreen[sline]->v_flag &= ~VFREQ;
  417.         vtmove(sline, -taboff);
  418.         if (lp != wp->w_bufp->b_linep) {
  419.             /* if we are not at the end */
  420.             for (i=0; i < llength(lp); ++i)
  421.                 vtputc(lgetc(lp, i));
  422.             lp = lforw(lp);
  423.         }
  424.  
  425.         /* make sure we are on screen */
  426.         if (vtcol < 0)
  427.             vtcol = 0;
  428.  
  429.         /* on to the next one */
  430. #if    COLOR
  431.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  432.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  433. #endif
  434.         vteeol();
  435.         ++sline;
  436.     }
  437.     taboff = 0;
  438. }
  439.  
  440. /*    updpos: update the position of the hardware cursor and handle extended
  441.         lines. This is the only update for simple moves.    */
  442.  
  443. PASCAL NEAR updpos()
  444.  
  445. {
  446.     register LINE *lp;
  447.     register int c;
  448.     register int i;
  449.  
  450.     /* find the current row */
  451.     lp = curwp->w_linep;
  452.     currow = curwp->w_toprow;
  453.     while (lp != curwp->w_dotp) {
  454.         ++currow;
  455.         lp = lforw(lp);
  456.     }
  457.  
  458.     /* find the current column */
  459.     curcol = 0;
  460.     i = 0;
  461.     while (i < curwp->w_doto) {
  462.         c = lgetc(lp, i++);
  463.         if (c == '\t')
  464.             curcol += - (curcol % tabsize) + (tabsize - 1);
  465.         else
  466.             if (c < 0x20 || c == 0x7f)
  467.                 ++curcol;
  468.  
  469.         ++curcol;
  470.     }
  471.  
  472.     /* adjust by the current first column position */
  473.     curcol -= curwp->w_fcol;
  474.  
  475.     /* make sure it is not off the left side of the screen */
  476.     while (curcol < 0) {
  477.         if (curwp->w_fcol >= hjump) {
  478.             curcol += hjump;
  479.             curwp->w_fcol -= hjump;
  480.         } else {
  481.             curcol += curwp->w_fcol;
  482.             curwp->w_fcol = 0;
  483.         }
  484.         curwp->w_flag |= WFHARD | WFMODE;
  485.     }
  486.  
  487.     /* if horizontall scrolling is enabled, shift if needed */
  488.     if (hscroll) {
  489.         while (curcol >= term.t_ncol - 1) {
  490.             curcol -= hjump;
  491.             curwp->w_fcol += hjump;
  492.             curwp->w_flag |= WFHARD | WFMODE;
  493.         }
  494.     } else {
  495.     /* if extended, flag so and update the virtual line image */
  496.         if (curcol >=  term.t_ncol - 1) {
  497.             vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  498.             updext();
  499.         } else
  500.             lbound = 0;
  501.     }
  502.  
  503.     /* update the current window if we have to move it around */
  504.     if (curwp->w_flag & WFHARD)
  505.         updall(curwp);
  506.     if (curwp->w_flag & WFMODE)
  507.         modeline(curwp);
  508.     curwp->w_flag = 0;
  509. }
  510.  
  511. /*    upddex: de-extend any line that derserves it        */
  512.  
  513. PASCAL NEAR upddex()
  514.  
  515. {
  516.     register WINDOW *wp;
  517.     register LINE *lp;
  518.     register int i,j;
  519.     register int nlines;    /* number of lines in the current window */
  520.  
  521.     wp = wheadp;
  522.  
  523.     while (wp != NULL) {
  524.         lp = wp->w_linep;
  525.         i = wp->w_toprow;
  526.         nlines = wp->w_ntrows;
  527.         if (modeflag == FALSE)
  528.             nlines++;
  529.  
  530.         while (i < wp->w_toprow + nlines) {
  531.             if (vscreen[i]->v_flag & VFEXT) {
  532.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  533.                    (curcol < term.t_ncol - 1)) {
  534.                     taboff = wp->w_fcol;
  535.                     vtmove(i, -taboff);
  536.                     for (j = 0; j < llength(lp); ++j)
  537.                         vtputc(lgetc(lp, j));
  538.                     vteeol();
  539.                     taboff = 0;
  540.  
  541.                     /* this line no longer is extended */
  542.                     vscreen[i]->v_flag &= ~VFEXT;
  543.                     vscreen[i]->v_flag |= VFCHG;
  544.                 }
  545.             }
  546.             lp = lforw(lp);
  547.             ++i;
  548.         }
  549.         /* and onward to the next window */
  550.         wp = wp->w_wndp;
  551.     }
  552. }
  553.  
  554. /*    updgar: if the screen is garbage, clear the physical screen and
  555.         the virtual screen and force a full update        */
  556.  
  557. PASCAL NEAR updgar()
  558.  
  559. {
  560.     register int i;
  561. #if    MEMMAP == 0
  562.     register int j;
  563.     register char *txt;
  564. #endif
  565.  
  566.     for (i = 0; i < term.t_nrow; ++i) {
  567.         vscreen[i]->v_flag |= VFCHG;
  568. #if    REVSTA
  569.         vscreen[i]->v_flag &= ~VFREV;
  570. #endif
  571. #if    COLOR
  572.         vscreen[i]->v_fcolor = gfcolor;
  573.         vscreen[i]->v_bcolor = gbcolor;
  574. #endif
  575. #if    MEMMAP == 0
  576.         txt = pscreen[i]->v_text;
  577.         for (j = 0; j < term.t_ncol; ++j)
  578.             txt[j] = ' ';
  579. #endif
  580.     }
  581.  
  582.     movecursor(0, 0);         /* Erase the screen. */
  583.     (*term.t_eeop)();
  584.     sgarbf = FALSE;          /* Erase-page clears */
  585.     mpresf = FALSE;          /* the message area. */
  586. #if    COLOR
  587.     mlerase();            /* needs to be cleared if colored */
  588. #endif
  589. }
  590.  
  591. /*    updupd: update the physical screen from the virtual screen    */
  592.  
  593. PASCAL NEAR updupd(force)
  594.  
  595. int force;    /* forced update flag */
  596.  
  597. {
  598.     register VIDEO *vp1;
  599.     register int i;
  600.  
  601.     for (i = 0; i < term.t_nrow; ++i) {
  602.         vp1 = vscreen[i];
  603.  
  604.         /* for each line that needs to be updated*/
  605.         if ((vp1->v_flag & VFCHG) != 0) {
  606. #if    TYPEAH
  607.             if (force == FALSE && typahead())
  608.                 return(TRUE);
  609. #endif
  610. #if    MEMMAP
  611.             updateline(i, vp1);
  612. #else
  613.             updateline(i, vp1, pscreen[i]);
  614. #endif
  615.         }
  616.     }
  617.     return(TRUE);
  618. }
  619.  
  620. /*    updext: update the extended line which the cursor is currently
  621.         on at a column greater than the terminal width. The line
  622.         will be scrolled right or left to let the user see where
  623.         the cursor is
  624.                                 */
  625. PASCAL NEAR updext()
  626.  
  627. {
  628.     register int rcursor;    /* real cursor location */
  629.     register LINE *lp;    /* pointer to current line */
  630.     register int j;     /* index into line */
  631.  
  632.     /* calculate what column the real cursor will end up in */
  633.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz)
  634.             + term.t_margin;
  635.     lbound = curcol - rcursor + 1;
  636.     taboff = lbound + curwp->w_fcol;
  637.  
  638.     /* scan through the line outputing characters to the virtual screen */
  639.     /* once we reach the left edge                    */
  640.     vtmove(currow, -taboff); /* start scanning offscreen */
  641.     lp = curwp->w_dotp;        /* line to output */
  642.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  643.         vtputc(lgetc(lp, j));
  644.  
  645.     /* truncate the virtual line, restore tab offset */
  646.     vteeol();
  647.     taboff = 0;
  648.  
  649.     /* and put a '$' in column 1 */
  650.     vscreen[currow]->v_text[0] = '$';
  651. }
  652.  
  653. /*
  654.  * Update a single line. This does not know how to use insert or delete
  655.  * character sequences; we are using VT52 functionality. Update the physical
  656.  * row and column variables. It does try an exploit erase to end of line. The
  657.  * RAINBOW version of this routine uses fast video.
  658.  */
  659. #if    MEMMAP
  660. /*    UPDATELINE specific code for the IBM-PC and other compatables */
  661.  
  662. PASCAL NEAR updateline(row, vp1)
  663.  
  664. int row;        /* row of screen to update */
  665. struct VIDEO *vp1;    /* virtual screen image */
  666.  
  667. {
  668. #if    COLOR
  669.     scwrite(row, vp1->v_text, vp1->v_rfcolor, vp1->v_rbcolor);
  670.     vp1->v_fcolor = vp1->v_rfcolor;
  671.     vp1->v_bcolor = vp1->v_rbcolor;
  672. #else
  673.     if (vp1->v_flag & VFREQ)
  674.         scwrite(row, vp1->v_text, 0, 7);
  675.     else
  676.         scwrite(row, vp1->v_text, 7, 0);
  677. #endif
  678.     vp1->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  679.  
  680. }
  681.  
  682. #else
  683.  
  684. PASCAL NEAR updateline(row, vp1, vp2)
  685.  
  686. int row;        /* row of screen to update */
  687. struct VIDEO *vp1;    /* virtual screen image */
  688. struct VIDEO *vp2;    /* physical screen image */
  689.  
  690. {
  691. #if RAINBOW
  692. /*    UPDATELINE specific code for the DEC rainbow 100 micro    */
  693.  
  694.     register char *cp1;
  695.     register char *cp2;
  696.     register int nch;
  697.  
  698.     /* since we don't know how to make the rainbow do this, turn it off */
  699.     flags &= (~VFREV & ~VFREQ);
  700.  
  701.     cp1 = &vp1->v_text[0];              /* Use fast video. */
  702.     cp2 = &vp2->v_text[0];
  703.     putline(row+1, 1, cp1);
  704.     nch = term.t_ncol;
  705.  
  706.     do
  707.     {
  708.     *cp2 = *cp1;
  709.     ++cp2;
  710.     ++cp1;
  711.     }
  712.     while (--nch);
  713.     *flags &= ~VFCHG;
  714. #else
  715. /*    UPDATELINE code for all other versions        */
  716.  
  717.     register char *cp1;
  718.     register char *cp2;
  719.     register char *cp3;
  720.     register char *cp4;
  721.     register char *cp5;
  722.     register int nbflag;    /* non-blanks to the right flag? */
  723.     int rev;        /* reverse video flag */
  724.     int req;        /* reverse video request flag */
  725.     int upcol;        /* update column (KRS) */
  726.  
  727.     /* set up pointers to virtual and physical lines */
  728.     cp1 = &vp1->v_text[0];
  729.     cp2 = &vp2->v_text[0];
  730.  
  731. #if    COLOR
  732.     TTforg(vp1->v_rfcolor);
  733.     TTbacg(vp1->v_rbcolor);
  734. #endif
  735.  
  736. #if    REVSTA | COLOR
  737.     /* if we need to change the reverse video status of the
  738.        current line, we need to re-write the entire line     */
  739.     rev = (vp1->v_flag & VFREV) == VFREV;
  740.     req = (vp1->v_flag & VFREQ) == VFREQ;
  741.     if ((rev != req)
  742. #if    COLOR
  743.         || (vp1->v_fcolor != vp1->v_rfcolor) || (vp1->v_bcolor != vp1->v_rbcolor)
  744. #endif
  745. #if    HP150
  746.     /* the HP150 has some reverse video problems */
  747.         || req || rev
  748. #endif
  749.             ) {
  750.         movecursor(row, 0);    /* Go to start of line. */
  751.         /* set rev video if needed */
  752.         if (rev != req)
  753.             (*term.t_rev)(req);
  754.  
  755.         /* scan through the line and dump it to the screen and
  756.            the virtual screen array                */
  757.         cp3 = &vp1->v_text[term.t_ncol];
  758.         while (cp1 < cp3) {
  759.             TTputc(*cp1);
  760.             ++ttcol;
  761.             *cp2++ = *cp1++;
  762.         }
  763.         /* turn rev video off */
  764.         if (rev != req)
  765.             (*term.t_rev)(FALSE);
  766.  
  767.         /* update the needed flags */
  768.         vp1->v_flag &= ~VFCHG;
  769.         if (req)
  770.             vp1->v_flag |= VFREV;
  771.         else
  772.             vp1->v_flag &= ~VFREV;
  773. #if    COLOR
  774.         vp1->v_fcolor = vp1->v_rfcolor;
  775.         vp1->v_bcolor = vp1->v_rbcolor;
  776. #endif
  777.         return(TRUE);
  778.     }
  779. #endif
  780.  
  781.     upcol = 0;
  782.  
  783.     /* advance past any common chars at the left */
  784.     while (cp1 != &vp1->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  785.         ++cp1;
  786.         ++upcol;
  787.         ++cp2;
  788.     }
  789.  
  790. /* This can still happen, even though we only call this routine on changed
  791.  * lines. A hard update is always done when a line splits, a massive
  792.  * change is done, or a buffer is displayed twice. This optimizes out most
  793.  * of the excess updating. A lot of computes are used, but these tend to
  794.  * be hard operations that do a lot of update, so I don't really care.
  795.  */
  796.     /* if both lines are the same, no update needs to be done */
  797.     if (cp1 == &vp1->v_text[term.t_ncol]) {
  798.         vp1->v_flag &= ~VFCHG;        /* flag this line is changed */
  799.         return(TRUE);
  800.     }
  801.  
  802.     /* find out if there is a match on the right */
  803.     nbflag = FALSE;
  804.     cp3 = &vp1->v_text[term.t_ncol];
  805.     cp4 = &vp2->v_text[term.t_ncol];
  806.  
  807.     while (cp3[-1] == cp4[-1]) {
  808.         --cp3;
  809.         --cp4;
  810.         if (cp3[0] != ' ')        /* Note if any nonblank */
  811.             nbflag = TRUE;        /* in right match. */
  812.     }
  813.  
  814.     cp5 = cp3;
  815.  
  816.     /* Erase to EOL ? */
  817.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  818.         while (cp5!=cp1 && cp5[-1]==' ')
  819.             --cp5;
  820.  
  821.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  822.             cp5 = cp3;        /* fewer characters. */
  823.     }
  824.  
  825.     /* movecursor(row, (int)(cp1 - &vp1->v_text[0]));  Go to start of line. */
  826.     movecursor(row, upcol);
  827. #if    REVSTA
  828.     TTrev(rev);
  829. #endif
  830.  
  831.     while (cp1 != cp5) {        /* Ordinary. */
  832.         TTputc(*cp1);
  833.         ++ttcol;
  834.         *cp2++ = *cp1++;
  835.     }
  836.  
  837.     if (cp5 != cp3) {        /* Erase. */
  838.         TTeeol();
  839.         while (cp1 != cp3)
  840.             *cp2++ = *cp1++;
  841.     }
  842. #if    REVSTA
  843.     TTrev(FALSE);
  844. #endif
  845.     vp1->v_flag &= ~VFCHG;        /* flag this line as updated */
  846.     return(TRUE);
  847. #endif
  848. }
  849. #endif
  850.  
  851. /*
  852.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  853.  * only routine that has any idea of how the modeline is formatted. You can
  854.  * change the modeline format by hacking at this routine. Called by "update"
  855.  * any time there is a dirty window.
  856.  */
  857. PASCAL NEAR modeline(wp)
  858.  
  859. WINDOW *wp;    /* window to update modeline for */
  860.  
  861. {
  862.     register char *cp;
  863.     register int c;
  864.     register int n;        /* cursor position count */
  865.     register BUFFER *bp;
  866.     register int i;        /* loop index */
  867.     register int lchar;     /* character to draw line in buffer with */
  868.     register int firstm;    /* is this the first mode? */
  869.     char tline[NLINE];        /* buffer for part of mode line */
  870.  
  871.     /* don't bother if there is none! */
  872.     if (modeflag == FALSE)
  873.         return;
  874.  
  875.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  876. #if    COLOR
  877.     vscreen[n]->v_flag |= VFCHG | VFCOL;        /* Redraw next time. */
  878.     vscreen[n]->v_rfcolor = 0;            /* black on */
  879.     vscreen[n]->v_rbcolor = 7;            /* white.....*/
  880. #else
  881.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;    /* Redraw next time. */
  882. #endif
  883.     vtmove(n, 0);                /* Seek to right line. */
  884.     if (wp == curwp)            /* mark the current buffer */
  885.         lchar = '=';
  886.     else
  887. #if    REVSTA
  888.     if (revexist)
  889.         lchar = ' ';
  890.     else
  891. #endif
  892.         lchar = '-';
  893.  
  894.     bp = wp->w_bufp;
  895.     if ((bp->b_flag&BFTRUNC) != 0)        /* "#" if truncated */
  896.         vtputc('#');
  897.     else
  898.         vtputc(lchar);
  899.  
  900.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  901.         vtputc('*');
  902.     else
  903.         vtputc(lchar);
  904.  
  905.     if ((bp->b_flag&BFNAROW) != 0) {        /* "<>" if narrowed */
  906.         vtputc('<');
  907.         vtputc('>');
  908.     } else {
  909.         vtputc(lchar);
  910.         vtputc(lchar);
  911.     }
  912.  
  913.     n  = 4;
  914.     strcpy(tline, " ");             /* Buffer name. */
  915.     strcat(tline, PROGNAME);
  916.     strcat(tline, " ");
  917.     strcat(tline, VERSION);
  918.     strcat(tline, " ");
  919.  
  920.     /* are we horizontally scrolled? */
  921.     if (wp->w_fcol > 0) {
  922.         strcat(tline, "[<");
  923.         strcat(tline, int_asc(wp->w_fcol));
  924.         strcat(tline, "]");
  925.     }
  926.  
  927.     /* display the modes */
  928.     strcat(tline, "(");
  929.     firstm = TRUE;
  930.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  931.         if (wp->w_bufp->b_mode & (1 << i)) {
  932.             if (firstm != TRUE)
  933.                 strcat(tline, " ");
  934.             firstm = FALSE;
  935.             strcat(tline, modename[i]);
  936.         }
  937.     strcat(tline,") ");
  938.  
  939.     cp = &tline[0];
  940.     while ((c = *cp++) != 0) {
  941.         vtputc(c);
  942.         ++n;
  943.     }
  944.  
  945. #if 0
  946.     vtputc(lchar);
  947.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  948.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  949.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  950.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  951.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  952.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  953.     vtputc(lchar);
  954.     n += 8;
  955. #endif
  956.  
  957.     vtputc(lchar);
  958.     vtputc(lchar);
  959.     vtputc(' ');
  960.     n += 3;
  961.     cp = &bp->b_bname[0];
  962.  
  963.     while ((c = *cp++) != 0) {
  964.         vtputc(c);
  965.         ++n;
  966.     }
  967.  
  968.     vtputc(' ');
  969.     vtputc(lchar);
  970.     vtputc(lchar);
  971.     n += 3;
  972.  
  973.     if (bp->b_fname[0] != 0) {    /* File name. */
  974.         vtputc(' ');
  975.         ++n;
  976.         cp = TEXT34;
  977. /*                   "File: " */
  978.  
  979.         while ((c = *cp++) != 0) {
  980.             vtputc(c);
  981.             ++n;
  982.         }
  983.  
  984.         cp = &bp->b_fname[0];
  985.  
  986.         while ((c = *cp++) != 0) {
  987.             vtputc(c);
  988.             ++n;
  989.             }
  990.  
  991.         vtputc(' ');
  992.         ++n;
  993.     }
  994.  
  995.     while (n < term.t_ncol) {    /* Pad to full width. */
  996.         vtputc(lchar);
  997.         ++n;
  998.     }
  999. }
  1000.  
  1001. PASCAL NEAR upmode()    /* update all the mode lines */
  1002.  
  1003. {
  1004.     register WINDOW *wp;
  1005.  
  1006.     wp = wheadp;
  1007.     while (wp != NULL) {
  1008.         wp->w_flag |= WFMODE;
  1009.         wp = wp->w_wndp;
  1010.     }
  1011. }
  1012.  
  1013. PASCAL NEAR upwind()    /* force hard updates on all windows */
  1014.  
  1015. {
  1016.     register WINDOW *wp;
  1017.  
  1018.     wp = wheadp;
  1019.     while (wp != NULL) {
  1020.         wp->w_flag |= WFHARD|WFMODE;
  1021.         wp = wp->w_wndp;
  1022.     }
  1023. }
  1024.  
  1025. /*
  1026.  * Send a command to the terminal to move the hardware cursor to row "row"
  1027.  * and column "col". The row and column arguments are origin 0. Optimize out
  1028.  * random calls. Update "ttrow" and "ttcol".
  1029.  */
  1030. PASCAL NEAR movecursor(row, col)
  1031.     {
  1032.     if (row!=ttrow || col!=ttcol)
  1033.     {
  1034.     ttrow = row;
  1035.     ttcol = col;
  1036.     TTmove(row, col);
  1037.     }
  1038.     }
  1039.  
  1040. /*
  1041.  * Erase the message line. This is a special routine because the message line
  1042.  * is not considered to be part of the virtual screen. It always works
  1043.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1044.  */
  1045. PASCAL NEAR mlerase()
  1046.     {
  1047.     int i;
  1048.     
  1049.     movecursor(term.t_nrow, 0);
  1050.     if (discmd == FALSE)
  1051.     return;
  1052.  
  1053. #if    COLOR
  1054.      TTforg(7);
  1055.      TTbacg(0);
  1056. #endif
  1057.     if (eolexist == TRUE)
  1058.         TTeeol();
  1059.     else {
  1060.     for (i = 0; i < term.t_ncol - 1; i++)
  1061.         TTputc(' ');
  1062.     movecursor(term.t_nrow, 1);    /* force the move! */
  1063.     movecursor(term.t_nrow, 0);
  1064.     }
  1065.     TTflush();
  1066.     mpresf = FALSE;
  1067.     }
  1068.  
  1069. /*
  1070.  * Write a message into the message line. Keep track of the physical cursor
  1071.  * position. A small class of printf like format items is handled. Assumes the
  1072.  * stack grows down; this assumption is made by the "+=" in the argument scan
  1073.  * loop. If  STACK_GROWS_UP  is set in estruct.h, then we'll assume that the
  1074.  * stack grows up and use "-=" instead of "+=". Set the "message line"
  1075.  *  flag TRUE.  Don't write beyond the end of the current terminal width.
  1076.  */
  1077.  
  1078. PASCAL NEAR mlout(c)
  1079.  
  1080. int c;    /* character to write on modeline */
  1081.  
  1082. {
  1083.     if (ttcol + 1 < term.t_ncol)
  1084.         TTputc(c);
  1085.     *lastptr++ = c;
  1086. }
  1087.  
  1088. #if    STACK_GROWS_UP
  1089. #define    ADJUST(ptr, dtype)    ptr -= sizeof(dtype)
  1090. #else
  1091. #define    ADJUST(ptr, dtype)    ptr += sizeof(dtype)
  1092. #endif
  1093.  
  1094. CDECL NEAR mlwrite(fmt, arg)
  1095.  
  1096. char *fmt;    /* format string for output */
  1097. char *arg;    /* pointer to first argument to print */
  1098.  
  1099. {
  1100.     register int c;     /* current char in format string */
  1101.     register char *ap;    /* ptr to current data field */
  1102.  
  1103.     /* if we are not currently echoing on the command line, abort this */
  1104.     if (discmd == FALSE)
  1105.         return;
  1106.  
  1107. #if    COLOR
  1108.     /* set up the proper colors for the command line */
  1109.     TTforg(7);
  1110.     TTbacg(0);
  1111. #endif
  1112.  
  1113.     /* if we can not erase to end-of-line, do it manually */
  1114.     if (eolexist == FALSE) {
  1115.         mlerase();
  1116.         TTflush();
  1117.     }
  1118.  
  1119.     movecursor(term.t_nrow, 0);
  1120.      lastptr = &lastmesg[0];        /* setup to record message */
  1121.     ap = (char *) &arg;
  1122.     while ((c = *fmt++) != 0) {
  1123.         if (c != '%') {
  1124.             mlout(c);
  1125.             ++ttcol;
  1126.         } else {
  1127.             c = *fmt++;
  1128.             switch (c) {
  1129.                 case 'd':
  1130.                     mlputi(*(int *)ap, 10);
  1131.                             ADJUST(ap, int);
  1132.                     break;
  1133.  
  1134.                 case 'o':
  1135.                     mlputi(*(int *)ap,  8);
  1136.                     ADJUST(ap, int);
  1137.                     break;
  1138.  
  1139.                 case 'x':
  1140.                     mlputi(*(int *)ap, 16);
  1141.                     ADJUST(ap, int);
  1142.                     break;
  1143.  
  1144.                 case 'D':
  1145.                     mlputli(*(long *)ap, 10);
  1146.                     ADJUST(ap, long);
  1147.                     break;
  1148.  
  1149.                 case 's':
  1150.                     mlputs(*(char **)ap);
  1151.                     ADJUST(ap, char *);
  1152.                     break;
  1153.  
  1154.                 case 'f':
  1155.                     mlputf(*(int *)ap);
  1156.                     ADJUST(ap, int);
  1157.                     break;
  1158.  
  1159.                 default:
  1160.                     mlout(c);
  1161.                     ++ttcol;
  1162.             }
  1163.         }
  1164.     }
  1165.  
  1166.     /* if we can, erase to the end of screen */
  1167.     if (eolexist == TRUE)
  1168.         TTeeol();
  1169.     TTflush();
  1170.     mpresf = TRUE;
  1171.     *lastptr = 0;    /* terminate lastmesg[] */
  1172. }
  1173.  
  1174. /*    Force a string out to the message line regardless of the
  1175.     current $discmd setting. This is needed when $debug is TRUE
  1176.     and for the write-message and clear-message-line commands
  1177. */
  1178.  
  1179. PASCAL NEAR mlforce(s)
  1180.  
  1181. char *s;    /* string to force out */
  1182.  
  1183. {
  1184.     register int oldcmd;    /* original command display flag */
  1185.  
  1186.     oldcmd = discmd;    /* save the discmd value */
  1187.     discmd = TRUE;        /* and turn display on */
  1188.     mlwrite(s);        /* write the string out */
  1189.     discmd = oldcmd;    /* and restore the original setting */
  1190. }
  1191.  
  1192. /*
  1193.  * Write out a string. Update the physical cursor position. This assumes that
  1194.  * the characters in the string all have width "1"; if this is not the case
  1195.  * things will get screwed up a little.
  1196.  */
  1197. PASCAL NEAR mlputs(s)
  1198.     char *s;
  1199.     {
  1200.     register int c;
  1201.  
  1202.     while ((c = *s++) != 0)
  1203.     {
  1204.     mlout(c);
  1205.     ++ttcol;
  1206.     }
  1207.     }
  1208.  
  1209. /*
  1210.  * Write out an integer, in the specified radix. Update the physical cursor
  1211.  * position.
  1212.  */
  1213. PASCAL NEAR mlputi(i, r)
  1214.     {
  1215.     register int q;
  1216.     static char hexdigits[] = "0123456789ABCDEF";
  1217.  
  1218.     if (i < 0)
  1219.     {
  1220.     i = -i;
  1221.     mlout('-');
  1222.     }
  1223.  
  1224.     q = i/r;
  1225.  
  1226.     if (q != 0)
  1227.     mlputi(q, r);
  1228.  
  1229.     mlout(hexdigits[i%r]);
  1230.     ++ttcol;
  1231.     }
  1232.  
  1233. /*
  1234.  * do the same except as a long integer.
  1235.  */
  1236. PASCAL NEAR mlputli(l, r)
  1237.     long l;
  1238.     {
  1239.     register long q;
  1240.  
  1241.     if (l < 0)
  1242.     {
  1243.     l = -l;
  1244.     mlout('-');
  1245.     }
  1246.  
  1247.     q = l/r;
  1248.  
  1249.     if (q != 0)
  1250.     mlputli(q, r);
  1251.  
  1252.     mlout((int)(l%r)+'0');
  1253.     ++ttcol;
  1254.     }
  1255.  
  1256. /*
  1257.  *    write out a scaled integer with two decimal places
  1258.  */
  1259.  
  1260. PASCAL NEAR mlputf(s)
  1261.  
  1262. int s;    /* scaled integer to output */
  1263.  
  1264. {
  1265.     int i;    /* integer portion of number */
  1266.     int f;    /* fractional portion of number */
  1267.  
  1268.     /* break it up */
  1269.     i = s / 100;
  1270.     f = s % 100;
  1271.  
  1272.     /* send out the integer portion */
  1273.     mlputi(i, 10);
  1274.     mlout('.');
  1275.     mlout((f / 10) + '0');
  1276.     mlout((f % 10) + '0');
  1277.     ttcol += 3;
  1278. }       
  1279.  
  1280. #if RAINBOW
  1281.  
  1282. PASCAL NEAR putline(row, col, buf)
  1283.     int row, col;
  1284.     char buf[];
  1285.     {
  1286.     int n;
  1287.  
  1288.     n = strlen(buf);
  1289.     if (col + n - 1 > term.t_ncol)
  1290.     n = term.t_ncol - col + 1;
  1291.     Put_Data(row, col, n, buf);
  1292.     }
  1293. #endif
  1294.  
  1295.